﻿#define _CRT_SECURE_NO_WARNINGS  1
//2331.计算布尔⼆叉树的值

 struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  };
 
class Solution {
public:
    bool evaluateTree(TreeNode* root)
    {
        return dfs(root);
    }

    bool dfs(TreeNode* root)
    {
        if (root->left == nullptr)
        {
            return root->val == 0 ? false : true;
        }
        bool left = dfs(root->left);
        bool right = dfs(root->right);

        return root->val == 2 ? left || right : left && right;

    }
};
//129.求根节点到叶节点数字之和
    class Solution {
    public:
        int sumNumbers(TreeNode* root)
        {
            return dfs(root, 0);
        }
        int dfs(TreeNode* root, int presum)
        {
            presum = presum * 10 + root->val;
            if (root->left == nullptr && root->right == nullptr)
                return presum;
            int ret = 0;
            if (root->left)
                ret += dfs(root->left, presum);
            if (root->right)
                ret += dfs(root->right, presum);

            return ret;
        }
};


